



// ----------------------------------------
//
// Basic Functions
//
// ----------------------------------------

function getObject(name) {
    if ("object" == typeof(name)) {
       return name; // this is actually the object itself, not its id
    }
	if (document.getElementById) {
	   	return document.getElementById(name);
 	}
 	else if (document.all) {
	   	return document.all[name];
 	}
 	else if (document.layers) {
	   	if (document.layers[name]) 
		{
	   		return document.layers[name];
		}
		else 
		{
	    	return document.layers.testP.layers[name];
	   	}
 	}
}

function objectRect(name) {
	var obj = getObject(name);
	var curleft = curtop = 0;
	var width = obj.offsetWidth;
	var height = obj.offsetHeight;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft
		curtop = obj.offsetTop
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
		}
	}
	return [curleft,curtop,width,height];
}

// store oft used objects to avoid re-getting overhead
var objectKeeper = {}
var $$ = function(objId) {
	if (objectKeeper[objId] == undefined) {
		objectKeeper[objId] = $(objId)
	}
	return objectKeeper[objId]
}

var goLoc = function(loc) {
	document.location = loc
}

function SetText(id, newtext) {
	var el = getObject(id);
	el.firstChild.data = newtext;
}

function GetValue(id) {
	var input = getObject(id);
	if (input) {
		return input.value;
	} else {
		return '';
	}
}
function SetValue(id, value) {
	var input = getObject(id);
	if (input) {
		input.value = value;
	} 
}
// ----------------------------------------
//
// Show/Hide Elements, Change Location and Size
//
// ----------------------------------------


function ToggleDisplay(id, forceClose) {
	var cal = getObject(id)
	var display = 'none'

	if (cal) {
		if (cal.style.display && !forceClose) {
			display = ''
		}
		cal.style.display = display
		return cal.style.display
	}	
}

function GetElementPosition(target) {
  if (typeof(target) == "string") {
      target = getObject(target)
  }
  if(target.offsetParent) {
    for(var posX = 0, posY = 0; target.offsetParent; target = target.offsetParent ) {
      posX += target.offsetLeft
      posY += target.offsetTop
    }
    return { left:posX, top:posY }
  } 
  else {
    return { left:target.offsetLeft, top:target.offsetTop }
  }
}

// because IE won't play nice. From Quirksmode
var findPosX = function (obj) {
	var curleft = 0;
	if (obj.offsetParent) {
		while (obj.offsetParent) {
			curleft += obj.offsetLeft
			obj = obj.offsetParent
		}
	}
	else if (obj.x)
		curleft += obj.x
	return curleft;
}

var findPosY = function (obj) {
	var curtop = 0;
	if (obj.offsetParent) {
		while (obj.offsetParent) {
			curtop += obj.offsetTop;
			obj = obj.offsetParent;
		}
	}
	else if (obj.y)
		curtop += obj.y;
	return curtop;
}

function ScrollToAndHighlight(id) {
	var obj = getObject(id)
	if (obj) {
		obj.className = "highlight"
		var loc = GetElementPosition(id)
		scrollTo(0, loc.top)
	}
}

// ----------------------------------------
//
// Controlling Element Opacity
//
// ----------------------------------------


function MakeInvisible(target) {
	target.style.visibility = 'hidden'
}

function MakeVisible(target) {
	target.style.visibility = 'visible'
}	
 
function SetOpacityById(id, opacity) {
	SetOpacity(getObject(id), opacity)
}

function SetOpacity(target, opacity) {
	/* the .001 fixes a glitch in the opacity calculation which normally
	 * results in a flash when reaching 1, but in a Mac absorbs 30% of the CPU */
	correction = navigator.userAgent.toLowerCase().indexOf('mac') ? 0 : .001;
	if (target.style.opacity!=null) {
		/* CSS3 compatible */
		target.style.opacity = (opacity/100)- correction;
	} else if (target.style.MozOpacity!=null) {
		/* Mozilla's pre-CSS3 proprietary rule */
		target.style.MozOpacity = (opacity/100)-correction;
	} else if (target.style.filter!=null) {
		/* IE's proprietary filter */
		if (opacity == 100)  {
			target.style.filter = "";
		} else  {
			target.style.filter = "alpha(opacity="+opacity+")";
		}
		/* worth noting: IE's opacity needs values in a range of 0-100, not 0.0 - 1.0 */ 
	}
}

function FadeIn(id, opacity) {
	var fadeTarget = getObject(id); 

	if (fadeTarget) {
		if (opacity <= 100) {
			SetOpacity(fadeTarget, opacity);			
			opacity += 10;
			setTimeout("FadeIn('"+id+"',"+opacity+")", 20);
		}
		else {
			MakeVisible(fadeTarget);
		}	
	}
}

function FadeOut(id, opacity) {
	var fadeTarget = getObject(id)

	if (fadeTarget) {
		if (opacity >= 0) {
			SetOpacity(fadeTarget, opacity)
			opacity -= 10
			setTimeout("FadeOut('"+id+"',"+opacity+")", 80)
		}
		else {
			MakeInvisible(fadeTarget)
		}
	}
}
	 
function FadeInAndOut(id, duration) {
	// given an id and a duration, fade id in, maintain for duration, and fade out 
	var fadeTarget = getObject(id)

	SetOpacity(fadeTarget, 0)
	fadeTarget.style.visibility = 'visible'
	setTimeout("FadeIn('"+id+"',0)", 0)
	setTimeout("FadeOut('"+id+"',100)", duration)
}

function clearFader(fadeTarget) {
	if (fadeTarget.fader) {
		fadeTarget.fader.stop()
	}
}

function startFadeInAndOut(id, duration) {
	var fadeTarget = getObject(id); 
	clearFader(fadeTarget);
	fadeTarget.fader = new FadeInOut(fadeTarget, duration);
}

function FadeInOut(target, duration) {
	this.opacity = 0
	this.max = duration / 4
	this.step = +10
	this.running = true;


	this.tick = function() {
		if (this.running) {
			opacity = Math.min(this.opacity, 100);
			opacity = Math.max(opacity, 0);
			SetOpacity(target, opacity);
			this.opacity += this.step;
			if (this.opacity > this.max) {
				this.step = -5;
			}
			if (this.opacity > 0) {
				this.setTimeout();
			}
			else {
				MakeInvisible(target);
			}
		}
	}

	this.setTimeout = function() {
		var t = this;
		var f = function () { t.tick(); }
		setTimeout( f, 40);
	}

	this.stop = function() {
		this.running = false;
	}

	this.setTimeout();
	SetOpacity(target, 0);
	MakeVisible(target);
}

function getkey(e) {
	if (window.event)
		return window.event.keyCode
	else if (e)
		return e.which
	else
		return null
}

function limitLength(fld, e, maxlen) {
	if (fld.value.length > maxlen) {
		var e = getkey(e) ;
		if ( e > 32 && e < 128) return false;
	}
	return true;
}

function limitLengthCountdown(limitField, limitCount, limitNum) {
	if (limitField.value.length > limitNum) {
		limitField.value = limitField.value.substring(0, limitNum);
	} else {
		limitCount.innerHTML = limitNum - limitField.value.length;
	}
}

function bothFunctions(a, b) {
	return function() { 
		if (a != undefined) a()
		if (b != undefined) b()
	}
}

function addOnloadFunction(f) {
	window.onload = bothFunctions(window.onload, f)
}

function createElement(parent, tagname) {
	var elem = document.createElement(tagname)
	if (parent) {
		parent.appendChild(elem);
	}
	return elem;
}

function clearChildren(d) {
	d = getObject(d);	  
	while (d && d.childNodes[0]) {
		d.removeChild(d.childNodes[0]);
	}
}

function createText(parent, msg) {
	var elem = document.createTextNode(msg);
	parent.appendChild(elem);
	return elem;
}

function setAttribute(node, name, value) {
	for (var i = 0; i < node.attributes.length; i++) {
		if (node.attributes[i].name == name) {
			node.attributes[i].value = value;
			return true;
		}
	}
	return false;
} 

function createInput(frm, itype) {
	// because lame, lame IE won't let me set a type directly 
	// or at all, if it has already been added to the parent
	var inputElement = (itype == "textarea") ? document.createElement("textarea") : document.createElement("input")
	if (!setAttribute(inputElement, "type", itype)) inputElement.type = itype
	frm.appendChild(inputElement)
	return inputElement
}

/* to compensate for IE weirdness in array definition */
function trimArray(ar) {
  while ( (ar.length > 0) &&  (ar[ar.length-1] == undefined) ) {
   ar.length--
  }
}

function strtrim(s) {
    return s.replace(/^\s+/,'').replace(/\s+$/,'')
}

function ShowUpdate(id, offsetl, offsett, text){
	if (text == undefined) text = "Updating..."
	var newLocation;
	var target = getObject("highlight")
	target.firstChild.innerHTML = text
	newLocation = GetElementPosition(id)
	target.style.left = (newLocation.left-offsetl) + 'px'
	target.style.top = (newLocation.top-offsett) + 'px'
	startFadeInAndOut("highlight", 1000 + text.length * 10)
}

var ie = (document.all) ? true : false

// Cookies
function createCookie(name,value,days) {
	if (days) {
		var date = new Date()
		date.setTime(date.getTime()+(days*24*60*60*1000))
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = ""
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++)
	{
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return '';
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}

var setActionCookie = function(viewID, actionID, domain) {
	document.cookie = "action." + domain + "=" + viewID + "|" + actionID + ";domain=" + domain + ";path=/";
	return true;
}

var addActionDetailCookie = function(detailID, detailValue, domain) {
	var details = getCurrentActionDetails(domain);
	details[detailID] = detailValue;
	setCurrentActionDetails(domain, details);
}

// used to translog a hit internal to the page or 
var internalTransLog = function(actionID, view_id) {
	asyncAction('userajax',
				{	
					xaction		: 'dotranslog',
					actionID 	: actionID,
					view_id   : view_id
				});
}

var getCurrentActionDetails = function(domain) {
	var re = new RegExp("actiondetails." + domain + "=([^;]*)");
	var current = re.exec(document.cookie);
	var details = {};
	if (current) {
		current = current[1].split(",");
		for (var c in current) {
			var v = current[c].split(":");
			if (v[0]) {
				details[v[0]] = v[1];
			}
		}
	}
	return details;
}

var setCurrentActionDetails = function(domain, details) {
	var sep = "";
	var s = ""
	for (var l in details) {
		s += sep + l + ":" + details[l];
		sep = ",";
	}
	document.cookie = "actiondetails." + domain + "=" + s + ";domain=" + domain + ";path=/";
};

var moveToFront = function(l, x) { 
	var rv=[ x ];
	for (var i in l) { 
		var n = l[i]; 
		if (n && (n != x)) rv.push(n);
	};
	return rv; 
}

var setSkinCookie = function(skinID, domain){
	var re = new RegExp("skins." + domain + "=([0-9:]+);");
	var ck = re.exec(document.cookie);
	var	skins = (ck && ck[1] != 'none') ? ck[1].split(":") : [ ] ;
	skins = moveToFront(skins, skinID);
	document.cookie = "skins." + domain + "=" + skins.join(":") + ";domain=" + domain + ";path=/";
}


var setLanguage = function(language, domain) {
	document.cookie = "language." + domain + "=" + language + ";domain=" + domain + ";path=/";
	document.location.reload(true);
	return false;
};

var valueOf = function(field) {
	if (field) {
		return field.type == 'checkbox' ? field.checked : field.value
	} else return null
}

// for writing flash out without hitting eolas snag
var writeHtml = function(objId, htmlCode) {
	$(objId).innerHTML = htmlCode
}

var bookmarkLink = function() {

	if( window.external ) { // ie
		window.external.AddFavorite( document.location, document.title)
	}
	return false;
 }

var makeEmailLink = function(localpart, text, domain) {
    var address = localpart + "@" + (domain || "slide.com");
	text = text || address;
	document.write("<a href='mailto:" + address + "'>" + text + "</a>");
};

var writeSimpleSlideShowTicker = function(target, site, channel_id, width, height) {
	var pathURL = "http://" + site + "/widgets/slideticker.swf";
	
	var fo = new FlashObject(pathURL, "flashticker", width, height, "8", "");
	fo.addVariable("site", site);
	fo.addVariable("channel", channel_id);
	fo.addVariable("nc", true);
	fo.addParam("quality", "high");
	fo.addParam("wmode", "transparent");
	fo.addParam("allowScriptAccess", "always");
	fo.addParam("salign", "l");
	fo.addParam("scale", "noscale");
	fo.addParam("align", "middle");
	fo.addParam("menu", "false");
	fo.write(target);
}
